<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <title>Netty WebSocket DEMO</title>
    <script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-3.4.1.min.js"></script>
    <style>
        .tanmuContent {
            justify-content: space-between;
        }
        .headImg img {
            width: 100%;
            height: 100%;
            border-radius: 50%;
        }
        .headImg {
            display: inline-block;
            width: 30px;
            height: 30px;
            background: red;
            border-radius: 41px;
        }
    </style>
</head>
<body>
<script type="text/javascript">
    var socket;
    if (!window.WebSocket) {
        window.WebSocket = window.MozWebSocket;
    }
    if (window.WebSocket) {
        socket = new WebSocket("ws://localhost:20001/ws");
        //连接创建成功时被回调
        socket.onopen = function(event) {
            // alert("websocket创建成功！");
        };
        //收到服务端的消息时被回调
        socket.onmessage = function(event) {
            showMsg(event.data);
        };
        socket.onclose = function(event) {
            var ta = document.getElementById('responseText');
            ta.value = ta.value + "连接被关闭";
        };
    } else {
        alert("你的浏览器不支持！");
    }

    function send(message) {
        if (!window.WebSocket) {
            return;
        }
        if (socket.readyState == WebSocket.OPEN) {
            socket.send(message);
        } else {
            alert("连接没有开启.");
        }
    }
    function showMsg(msg){
        var id="div_"+new Date().getTime();
        var html='<div id="'+id+'" class="tanmuContent" style="position: absolute;"><span class="headImg"><img src="tanmuhead.jpg"></span>'+msg+'<div class="praiseBox"><span class="t-praise "></span></div></div>';
        var height=$(document).height();
        var width=$(document.body).width();
        html =$(html);
        var t = Math.floor(Math.random()*(height-1+1)+1);
        html.css("left",width*0.8+"px")
        html.css("top",t+"px")

        $("body").append(html);
        $("#"+id).animate({"left":-width},20000,function(){
            $("#"+id).remove();
            console.log("sss")
        });


    }

</script>
<form onsubmit="return false;">
    <input type="text" name="message" value="Hello, World!"><input
    type="button" value="发送消息"
    onclick="send(this.form.message.value)">
</form>
</body>
</html>